/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui;

import BUSServices.NhaXuatBan;
import BUSServices.Sach;
import BUSServices.TheLoai;
import java.awt.Component;
import java.io.File;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

/**
 *
 * @author Admin
 */
public class ChonSach extends javax.swing.JDialog {

    BUSServices.QLNSBUSWebService_Service service;
    BUSServices.QLNSBUSWebService port;
    private DefaultComboBoxModel cbModelTheLoai;
    private DefaultComboBoxModel cbModelNXB;
    private List<Sach> gioHang;

    public ChonSach(java.awt.Dialog parent, List sachs, boolean modal) {
        super(parent, modal);
        initComponents();
        gioHang = sachs;

        this.cbModelNXB = new DefaultComboBoxModel();
        this.cbModelTheLoai = new DefaultComboBoxModel();
        //Khởi tạo services và gọi services.
        service = new BUSServices.QLNSBUSWebService_Service();
        port = service.getQLNSBUSWebServicePort();

        this.jtfMaSach.getDocument().addDocumentListener(new DocumentListener() {
            @Override
            public void insertUpdate(DocumentEvent e) {

                int maSach = Integer.parseInt(jtfMaSach.getText());
                String mySQL = String.format("select * from sach where ma_sach = %d", maSach);
                List<Sach> s = port.laySach(mySQL);
                if (s.size() == 0) {
                    jtfTacGia.setText("");
                    jtfGiaBan.setText("");
                    jtfNXB.setText("");
                    jtfSoLuong.setText("");
                    jtfTenSach.setText("");
                    jtfTheLoai.setText("");
                } else {
                    Sach sach = s.get(0);
                    int maTheLoai = sach.getMaTheLoai();
                    int maNXB = sach.getMaNhaXuatBan();

                    mySQL = String.format("select * from the_loai where ma_the_loai = %d", maTheLoai);
                    List<TheLoai> theLoais = port.layTheLoai(mySQL);

                    mySQL = String.format("select * from nha_xuat_ban where ma_nha_xuat_ban = %d", maNXB);
                    List<NhaXuatBan> nxbs = port.layNhaxuatBan(mySQL);

                    TheLoai theLoai = theLoais.get(0);
                    NhaXuatBan nxb = nxbs.get(0);

                    jtfTacGia.setText(sach.getTacGia());
                    jtfTenSach.setText(sach.getTenSach());
                    jtfGiaBan.setText("" + sach.getGiaBan());
                    jtfSoLuong.setText("" + sach.getSoLuongTon());
                    jtfTheLoai.setText(theLoai.getTenTheLoai());
                    jtfNXB.setText(nxb.getTenNXB());
                }



            }

            @Override
            public void removeUpdate(DocumentEvent e) {
                try {
                    int maSach = Integer.parseInt(jtfMaSach.getText());
                    String mySQL = String.format("select * from sach where ma_sach = %d", maSach);
                    List<Sach> s = port.laySach(mySQL);
                    if (s.size() == 0) {
                        jtfTacGia.setText("");
                        jtfGiaBan.setText("");
                        jtfNXB.setText("");
                        jtfSoLuong.setText("");
                        jtfTenSach.setText("");
                        jtfTheLoai.setText("");
                    } else {
                        Sach sach = s.get(0);
                        int maTheLoai = sach.getMaTheLoai();
                        int maNXB = sach.getMaNhaXuatBan();

                        mySQL = String.format("select * from the_loai where ma_the_loai = %d", maTheLoai);
                        List<TheLoai> theLoais = port.layTheLoai(mySQL);

                        mySQL = String.format("select * from nha_xuat_ban where ma_nha_xuat_ban = %d", maNXB);
                        List<NhaXuatBan> nxbs = port.layNhaxuatBan(mySQL);

                        TheLoai theLoai = theLoais.get(0);
                        NhaXuatBan nxb = nxbs.get(0);

                        jtfTacGia.setText(sach.getTacGia());
                        jtfTenSach.setText(sach.getTenSach());
                        jtfGiaBan.setText("" + sach.getGiaBan());
                        jtfSoLuong.setText("" + sach.getSoLuongTon());
                        jtfTheLoai.setText(theLoai.getTenTheLoai());
                        jtfNXB.setText(nxb.getTenNXB());
                    }
                }catch(Exception ex){
                    
                }
            }

            @Override
            public void changedUpdate(DocumentEvent e) {
                System.out.println("change");
            }
        });


    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane2 = new javax.swing.JScrollPane();
        jTextArea2 = new javax.swing.JTextArea();
        jLabel1 = new javax.swing.JLabel();
        jbtnDongY = new javax.swing.JButton();
        jtfSoLuong = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jtfMaSach = new javax.swing.JTextField();
        jLabel10 = new javax.swing.JLabel();
        jtfGiaBan = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jbtnThoat = new javax.swing.JButton();
        jtfTenSach = new javax.swing.JTextField();
        jtfTacGia = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        jtfSoLuongBan = new javax.swing.JTextField();
        jtfTheLoai = new javax.swing.JTextField();
        jtfNXB = new javax.swing.JTextField();
        jbtnTraCuu1 = new javax.swing.JButton();

        jTextArea2.setColumns(20);
        jTextArea2.setRows(5);
        jScrollPane2.setViewportView(jTextArea2);

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Nhập sách thường");

        jLabel1.setText("Tên Sách:");

        jbtnDongY.setText("Đồng ý");
        jbtnDongY.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnDongYActionPerformed(evt);
            }
        });

        jLabel4.setText("Tác Giả:");

        jLabel5.setText("Số lượng tồn:");

        jLabel6.setText("Thể Loại:");

        jLabel7.setText("Mã sách:");

        jLabel10.setText("Giá bán:");

        jLabel2.setText("Nhà xuất bản:");

        jbtnThoat.setText("Thoát");
        jbtnThoat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnThoatActionPerformed(evt);
            }
        });

        jtfTenSach.setDragEnabled(true);

        jtfTacGia.setDragEnabled(true);

        jLabel9.setText("Số lượng bán:");

        jbtnTraCuu1.setText("Tra cứu sách");
        jbtnTraCuu1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnTraCuu1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(22, 22, 22)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jLabel7)
                                    .addComponent(jLabel6))
                                .addGap(51, 51, 51)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(jtfMaSach, javax.swing.GroupLayout.DEFAULT_SIZE, 119, Short.MAX_VALUE)
                                    .addComponent(jtfTheLoai))
                                .addGap(18, 18, 18)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel4)
                                    .addComponent(jLabel1))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 31, Short.MAX_VALUE))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addGap(0, 0, Short.MAX_VALUE)
                                .addComponent(jbtnTraCuu1, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(12, 12, 12)))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jtfTenSach, javax.swing.GroupLayout.PREFERRED_SIZE, 222, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jtfTacGia, javax.swing.GroupLayout.PREFERRED_SIZE, 222, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(27, 27, 27)
                                .addComponent(jbtnDongY, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 31, Short.MAX_VALUE)
                                .addComponent(jbtnThoat, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(jLabel9)
                                    .addGap(28, 28, 28)
                                    .addComponent(jtfSoLuongBan, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jLabel5)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(jtfSoLuong, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                        .addComponent(jLabel10)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 53, Short.MAX_VALUE)
                                        .addComponent(jtfGiaBan, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(0, 0, Short.MAX_VALUE)
                                        .addComponent(jtfNXB, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE))))
                            .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.LEADING))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jtfGiaBan, jtfMaSach, jtfNXB, jtfSoLuong, jtfSoLuongBan, jtfTheLoai});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(25, 25, 25)
                        .addComponent(jtfTenSach, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(26, 26, 26)
                        .addComponent(jtfTacGia, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(20, 20, 20)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel7)
                            .addComponent(jtfMaSach, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel1))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel6)
                            .addComponent(jtfTheLoai, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(jtfNXB, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(3, 3, 3)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jtfGiaBan, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel10))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jtfSoLuong, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel5)))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(9, 9, 9)
                                .addComponent(jLabel4)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel9)
                            .addComponent(jtfSoLuongBan, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jbtnThoat, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jbtnDongY, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jbtnTraCuu1, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(0, 30, Short.MAX_VALUE))
        );

        layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {jtfGiaBan, jtfMaSach, jtfNXB, jtfSoLuong, jtfSoLuongBan, jtfTheLoai});

        pack();
    }// </editor-fold>//GEN-END:initComponents

    //Lưu sách vào cơ sở dữ liệu
    private void jbtnDongYActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnDongYActionPerformed
        try {
            Sach s = new Sach();
            s.setMaSach(Integer.parseInt(this.jtfMaSach.getText()));
            s.setTenSach(this.jtfTenSach.getText());
            s.setGiaBan(Float.parseFloat(this.jtfGiaBan.getText()));
            s.setSoLuongBan(Integer.parseInt(this.jtfSoLuongBan.getText()));

            gioHang.add(s);

        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, ex.getMessage());
        } finally {
            this.dispose();
        }

    }//GEN-LAST:event_jbtnDongYActionPerformed

    private void jbtnThoatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnThoatActionPerformed
        this.dispose();
    }//GEN-LAST:event_jbtnThoatActionPerformed

    private void jbtnTraCuu1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnTraCuu1ActionPerformed
        TimSach t = new TimSach();
        t.setTitle("Tra cứu thông tin sách");
        t.setVisible(true);
    }//GEN-LAST:event_jbtnTraCuu1ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(ChonSach.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ChonSach.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(ChonSach.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ChonSach.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                ChonSach dialog = new ChonSach(new javax.swing.JDialog(), new ArrayList<Sach>(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextArea jTextArea2;
    private javax.swing.JButton jbtnDongY;
    private javax.swing.JButton jbtnThoat;
    private javax.swing.JButton jbtnTraCuu1;
    private javax.swing.JTextField jtfGiaBan;
    private javax.swing.JTextField jtfMaSach;
    private javax.swing.JTextField jtfNXB;
    private javax.swing.JTextField jtfSoLuong;
    private javax.swing.JTextField jtfSoLuongBan;
    private javax.swing.JTextField jtfTacGia;
    private javax.swing.JTextField jtfTenSach;
    private javax.swing.JTextField jtfTheLoai;
    // End of variables declaration//GEN-END:variables
}
